<template>
  <div class="container">
    <uni-indexed-list :options="users" :showSelect="false" @click="bindClick"></uni-indexed-list>

    <uv-empty mode="data" text="暂无数据" v-if="!users.length"></uv-empty>
  </div>
</template>

<script setup lang="ts">
// #ifdef MP-WEIXIN
import { pinyin } from "pinyin-pro";
// #endif

const { proxy } = getCurrentInstance();
import useCounterStore from "@/stores/counter";
const counter = useCounterStore();
const instance = getCurrentInstance();

import YeIMStore from "@/stores/YeIM";
const YeIM = YeIMStore();

let page = 1;
let canLoadMore = true;
let list = ref([]);

onLoad(() => {
  getInfo();
});

onReachBottom(() => {
  getMore();
});

let users = computed(() => {
  return splitArrayByField(list.value, "nickname");
});

function getMore() {
  if (!canLoadMore) return;
  page++;
  getInfo();
}

function bindClick(e) {
  let userId = e.item.userId;
  proxy.$utils.toLink(`/packagePages/user/details?id=${userId}`);
}

async function getInfo() {
  let { data } = await YeIM.getFriendList(page);
  if (data.length) {
    for (let index = 0; index < data.length; index++) {
      const element = data[index];
      element.nickname = element.friendInfo.nickname;
    }
    list.value = proxy.$utils.filterArr([...list.value], data, "id");
  } else {
    canLoadMore = false;
  }
  console.log("aa", list.value);
}

function splitArrayByField(jsonArray, fieldName) {
  let array = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z",
  ];
  let result = [];
  array.forEach((element) => {
    result.push({
      letter: element,
      data: [],
    });
  });
  jsonArray.forEach((element) => {
    let firstEnglishChar = pinyin(element[fieldName]).charAt(0).toUpperCase();
    for (let index = 0; index < result.length; index++) {
      const item = result[index];
      if (item.letter == removeDiacritics(firstEnglishChar)) {
        item.data.push({
          name: element.friendInfo.nickname,
          pic: element.friendInfo.avatarUrl,
          userId:
            element.userId == counter.slParams.userInfo.userId
              ? element.friendUserId
              : element.userId,
        });
      }
    }
  });
  return result;
}

//去除音节
function removeDiacritics(str) {
  return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
</script>



<style lang="scss" scoped>
.container {
}
</style>



